package cn.myframe.controller.sys;

import cn.myframe.controller.AbstractController;
import cn.myframe.controller.BaseController;
import cn.myframe.entity.sys.SysRoleEntity;
import cn.myframe.entity.sys.SysUserEntity;
import cn.myframe.service.BaseService;
import cn.myframe.service.sys.SysRoleCompanyService;
import cn.myframe.service.sys.SysRoleMenuService;
import cn.myframe.service.sys.SysRoleService;
import cn.myframe.service.sys.SysUserService;
import cn.myframe.utils.annotation.CheckDelete;
import cn.myframe.utils.annotation.CheckNoPass;
import cn.myframe.utils.aop.AssociateData;
import cn.myframe.utils.core.PageUtils;
import cn.myframe.utils.core.Query;
import cn.myframe.utils.core.R;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 角色
 * 
 * @author ynz
 * @email ynz@myframe.cn
 * @date 2018-08-01 16:46:19
 */
@RestController
@RequestMapping("/sys/sysrole")
public class SysRoleController  extends AbstractController implements BaseController<SysRoleEntity,BaseService<SysRoleEntity>>{
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	@Autowired
	private SysRoleCompanyService sysRoleCompanyService;
	@Autowired
	private SysUserService sysUserService;

	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping(value = "/list",method = {RequestMethod.GET})
	@RequiresPermissions("sysrole:list")
	public R lists(@RequestParam Map<String, Object> params){
		Query query = new Query(params);
		PageUtils pageUtil = getService().queryPage(query);
		for (Object obj : pageUtil.getList()) {
			SysRoleEntity sysRoleEntity = (SysRoleEntity) obj;
			SysUserEntity userEntity = sysUserService.selectById(sysRoleEntity.getCreateBy());
			if (userEntity != null) sysRoleEntity.setCreateUserName(userEntity.getUsername());
		}
		return R.ok().put("page", pageUtil);
	}
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{roleId}")
	@RequiresPermissions("sysrole:info")
	public R info(@PathVariable("roleId") Long id){
		SysRoleEntity sysRoleEntity = getService().queryObject(id);
		List<Long> menuIdList = sysRoleMenuService.selectMenuIdList(sysRoleEntity.getRoleId());
		sysRoleEntity.setMenuIdList(menuIdList);
		List<Long> companyNoList = sysRoleCompanyService.selectCompanyList(sysRoleEntity.getRoleId());
		sysRoleEntity.setCompanyNoList(companyNoList);
		return R.ok().put("sysRole", sysRoleEntity);
	}
	
	/**
	 * 新增保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sysrole:save")
	public R save(@RequestBody SysRoleEntity sysRole){
		sysRoleService.insertWithPerms(sysRole);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sysrole:delete")
	@CheckDelete(AssociateData.ROLE_USER)
	public R delete(@RequestBody Long[] ids){
		sysRoleService.deleteWithPerms(ids);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")	
	@RequiresPermissions("sysrole:update")
	public R update(@RequestBody SysRoleEntity sysRole){
		SysRoleEntity oldObject = sysRoleService.queryObject(sysRole.getRoleId());
		sysRoleService.updateWithPerms(sysRole);
		return R.ok().put("oldObject", oldObject);
	}
	
   /**
    * 提交,审核
    */
	@RequestMapping("/audit")
	@CheckNoPass(AssociateData.ROLE_USER)
	@RequiresPermissions(value = {"sysrole:submit", "sysrole:auditpass", "sysrole:auditnopass"}, logical = Logical.OR)
	public R audit(@RequestBody Long[] ids ,@RequestParam Integer status){
		return this.defaultAudit(ids, status);
	}
	
   /**
	* 检验字段值重复
	*/
    @RequestMapping("/checkExist")
    @ResponseBody
    public String checkExist(String id,String value,String columnName){
        return this.defaultCheckExist(id, value, columnName);
    }
    
    public SysRoleService getService(){
		return sysRoleService;
	}
	
}
